package com.changan.utils;

import com.ejlchina.okhttps.HTTP;
import com.ejlchina.okhttps.HttpResult;
import com.ejlchina.okhttps.OkHttps;
import com.ejlchina.okhttps.jackson.JacksonMsgConvertor;
import okhttp3.OkHttpClient;

import java.util.Map;
import java.util.concurrent.TimeUnit;

public class HttpUtils {

    private static HTTP http = HTTP.builder().config(
            (OkHttpClient.Builder builder) -> {
                // 连接超时时间（默认10秒）
                builder.connectTimeout(60, TimeUnit.SECONDS);
                // 写入超时时间（默认10秒）
                builder.writeTimeout(10, TimeUnit.SECONDS);
                // 读取超时时间（默认10秒）
                builder.readTimeout(600, TimeUnit.SECONDS);
            }
    ).addMsgConvertor(new JacksonMsgConvertor()).build();

    /**
     * @param url http的get请求
     * @return 返回状态值
     */
    public static Integer httpGet(String url) {
        return http.sync(url).get().getStatus();
    }

    /**
     * http的patch请求,用来请求停止flink任务,成功将返回202
     *
     * @param url 请求的url
     * @return 状态码
     */
    public static int httpPatch(String url) {
        return http.sync(url).patch().getStatus();
    }

    /**
     * @param url http的get请求
     * @return 返回状态值
     */
    public static Integer httpGetWithBaseUrl(String baseUrl, String url) {
        HTTP http = HTTP.builder()
                .baseUrl(baseUrl)
                .addMsgConvertor(new JacksonMsgConvertor())
                .build();

        return http.sync(url).get().getStatus();
    }


    /**
     * HTTP GET请求返回BODY
     *
     * @param url get请求
     * @return 返回body
     */
    public static HttpResult.Body httpGetBody(String url) {
        return http.sync(url).get().getBody();
    }

    /**
     * @param url http的post方法访问的url
     * @return 返回状态值
     */
    public static String httpPost(String url) {
        HttpResult httpResult = http.sync(url).post();
        HttpResult.Body body = httpResult.getBody();
        return body.toString();
    }

    public static HttpResult httpPostGetBody(String url) {
        return http.sync(url).post();
    }

    /**
     * @param url    访问的url
     * @param params 请求参数
     * @return 状态值
     */
    public static String httpPost(String url, String params) {
        HttpResult httpResult = http.sync(url).setBodyPara(params).post();
        Integer status = httpResult.getStatus();
        return status.toString();
    }

    /**
     * @param url    访问的url
     * @param params 请求参数
     * @return body
     */
    public static String httpPost(String url, String params, String pass) {
        HttpResult httpResult = http.sync(url).setBodyPara(params).post();
        HttpResult.Body body = httpResult.getBody();
        String rspBody = body.toString();
        body.close();
        return rspBody;
    }

    /**
     * @param url    访问的url
     * @param params 请求参数
     * @return body
     */
    public static String httpPost(String url, Object params) {
        HttpResult httpResult = http.sync(url).bodyType(OkHttps.JSON).setBodyPara(params).post();
        return httpResult.getBody().toString();
    }

    public static String httpGet(String url, Map<String, String> headers) {
        HttpResult httpResult = http.sync(url).addHeader(headers).get();
        return httpResult.getBody().toString();
    }

}
